const Stack = require('./stack')

// 4+13/5
const exp1 = ['4', '13', '5', '/', '+']
const exp2 = ['10', '6', '9', '3', '+', '-11', '*', '/', '*', '17', '+', '5', '+']

function calcExp(exp) {
  const stack = new Stack()
  for (let i = 0; i < exp.length; i++) {
    const item = exp[i]
    if (['+', '-', '*', '/'].indexOf(item) >= 0) {
      const value1 = stack.pop()
      const value2 = stack.pop()
      const expStr = value2 + item + value1
      stack.push(parseInt(eval(expStr)).toString())
    } else {
      stack.push(item)
    }
  }
  return stack.pop()
}

console.log(calcExp(exp1))
console.log(calcExp(exp2))
